﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data;
using System.Data.SqlClient;

using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MODEL;


namespace DAL
{
    public class PicMakeUpDAL
    {
        private static readonly string SELECT_PICMAKEUP = "select ID,X,Y from picMakeUp where BoardId =@BoardId";
        private static readonly string UPDATE_PICMAKEUP = "update picMakeUp set x =@x,y =@y where id =@id";
        private static readonly string UPDATE_BOARD = "update Board set status =3 where BoardId=@BoardId ";
        public List<PicMakeUpInfo> GetPicMakeUpInfoList(int boardId)
        {

            List<PicMakeUpInfo> list = new List<PicMakeUpInfo>();

            DataTable dt = new DataTable();
            dt = ExcuteSqlText(SELECT_PICMAKEUP, boardId);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int id = Convert.ToInt32(dt.Rows[i]["ID"]);
                string x = dt.Rows[i]["X"].ToString();
                string y = dt.Rows[i]["Y"].ToString();
                PicMakeUpInfo picMakeUpInfo = new PicMakeUpInfo(id, x, y);
                list.Add(picMakeUpInfo);
            }

            return list;
        }

        public int Update(BoardInfo boardInfo)
        {
            int ret = 1;
            Database db = DatabaseFactory.CreateDatabase();
            using (DbConnection con = db.CreateConnection())
            {
                con.Open();
                DbTransaction tran = con.BeginTransaction();
                try
                {
                    DbCommand dbcommand = db.GetSqlStringCommand(UPDATE_PICMAKEUP);
                    foreach (var item in boardInfo.PicList)
                    {
                        if (dbcommand.Parameters.Count > 0)
                        {
                            dbcommand.Parameters.Clear();
                        }
                        db.AddInParameter(dbcommand, "x", DbType.String, item.X);
                        db.AddInParameter(dbcommand, "y", DbType.String, item.Y);
                        db.AddInParameter(dbcommand, "id", DbType.Int32, item.Id);

                        db.ExecuteNonQuery(dbcommand, tran);
                    }

                    dbcommand = db.GetSqlStringCommand(UPDATE_BOARD);
                    if (dbcommand.Parameters.Count > 0)
                    {
                        dbcommand.Parameters.Clear();
                    }
                    db.AddInParameter(dbcommand, "BoardId", DbType.Int32, boardInfo.BoardId);
                    db.ExecuteNonQuery(dbcommand, tran);

                    tran.Commit();
                }
                catch
                {
                    tran.Rollback();
                    ret = 1;
                }
                finally
                {
                    if (con.State != ConnectionState.Closed)
                    {
                        con.Close();
                    }

                }
            }
            return 0;
        }


        private DataTable ExcuteSqlText(string sqlText, int param)
        {
            try
            {
                DataTable dt = new DataTable();
                Database db = null;
                db = DatabaseFactory.CreateDatabase();
                DbCommand cmd = db.GetSqlStringCommand(sqlText);
                db.AddInParameter(cmd, "@BoardId", DbType.Int32, param);
                return db.ExecuteDataSet(cmd).Tables[0];
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
